capture program drop kl_cont
program define kl_cont

syntax [if] [in], ind_var(string) treatment(string) gen(string) prefix(integer) ///
				  sd(real)

local sd1 = round(`sd', 0.01)

display "Independent variable is `ind_var'"
display "Treatment variable is `treatment'"
display "New variable to be generated is `gen'`prefix'"
display "standard deviation specified is `sd1'"


//generating the new variable
gen `gen'`prefix' = `ind_var'  
label variable `gen'`prefix' "`ind_var' Imputation using `sd1' standard deviation"

**generate a random number to sort data before imputation
gen random = runiform()

**generate a dummy variable to identify missing enroll 
gen d_imp = 0
replace d_imp = 1 if `ind_var' ==.

**generate a ranking variable that will be used for imputation

bysort `treatment' d_imp (random): gen rank = _n


***Control group
**calculate mean and sd
summ `ind_var' if `treatment'==0
local sd_c = `r(sd)'*`sd1'  //0.5 sd
local mean_c = `r(mean)'     // mean

display `sd_c'
display `mean_c' 

local impu_c = `mean_c' + `sd_c'

replace `gen'`prefix'=`impu_c' if 	`treatment'==0 & `ind_var'==.


***Treatment group
**calculate mean and sd
summ `ind_var' if `treatment'==1
local sd_t = `r(sd)'*`sd1'  //0.5 sd
local mean_t = `r(mean)'     // mean

display `sd_t'
display `mean_t' 

local impu_t = `mean_t' - `sd_t'

replace `gen'`prefix'=`impu_t'  if `treatment'==1 & `ind_var'==.

drop random d_imp rank

end 